# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2022年01月20日
"""
import requests
import json
import os
import time

UA伪装 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
    'Referer': 'http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'csrf': 'RUJ53PGJ4ZD',
    'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1577029678,1577034191,1577034210,1577076651; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1577080777; kw_token=RUJ53PGJ4ZD'
}
歌手名称=input("请输入要下载的歌手:")
页数=int(input('请输入要下载的页数:'))
if not os.path.exists(f'e:/爬虫下载/{歌手名称}'):
	os.mkdir(f'e:/爬虫下载/{歌手名称}')

for i in range(1,页数+1):
	网址=f"http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={歌手名称}&pn={i}&rn=30&httpsStatus=1&reqId=9d140260-79f7-11ec-a7fb-936c2e1ec0d6"
	响应数据=requests.get(url=网址,headers=UA伪装).text
	字典=json.loads(响应数据)
	列表=字典['data']['list']
	for i in 列表:
		编码=i['rid']
		歌名=i['name']
		网址=f"http://www.kuwo.cn/api/v1/www/music/playUrl?mid={编码}&type=music&httpsStatus=1&reqId=8d8b46b1-79fb-11ec-96b5-7178fdd40485"
		响应数据=requests.get(url=网址,headers=UA伪装).text
		字典=json.loads(响应数据,strict=False)
		下载地址=字典['data']['url']
		mp3 = requests.get(url=下载地址).content
		路径=f'e:/爬虫下载/{歌手名称}/{歌名}.mp3'
		with open(路径,'wb') as f:
			f.write(mp3)
			print(歌名,'下载成功!')
			time(5)
print('over!')






